package com.foreknow.elm.controller;

import com.foreknow.elm.po.User;
import com.foreknow.elm.service.UserService;
import com.foreknow.elm.service.impl.UserServiceImpl;
import com.foreknow.elm.util.DBUtil;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;

/**
 * @author shkstart
 * @create 2021-08-26 14:21
 */
public class UserController {
    /**
     * 根据用户编号与密码查询用户信息
     * @return user对象
     */
    public User getUserByIdByPass(HttpServletRequest request)throws Exception{

        //获取请求参数  UserController/getUserByIdByPass?userId =?&password=?
        String userId=request.getParameter("userId");
        String password=request.getParameter("password");
        //调用service方法

        UserService service=new UserServiceImpl();
        User user = service.SvgetUserByIdByPass(userId, password);


        return user;
    }


    /**
     * 根据用户编号查询用户表返回的行数
     * @return 行数
     */
    public int getUserById(HttpServletRequest request)  throws Exception{
        //获取请求参数UserController/getUserById 参数：userId 返回值：int（返回行数）
        String userId = request.getParameter("userId");
        //调用service
        UserService service=new UserServiceImpl();
        int i = service.SvgetUserById(userId);


        return i;
    }


    /**
     * 向用户表中添加一条记录
     * @return （影响的行数）
     */
    public int saveUser(HttpServletRequest request)throws Exception {
        //获取请求参数 UserController/saveUser 参数：userId、password、userName、userSex 返回值：int（影响的行数）
        String userId=request.getParameter("userId");
        String password=request.getParameter("password");
        String userName=request.getParameter("userName");
        int userSex= Integer.parseInt(request.getParameter("userSex"));

        UserService service=new UserServiceImpl();
        int i = service.SvsaveUser(userId, password, userName, userSex);

        return i;
    }
}
